<?php
namespace app\app\controller;
use think\Controller;
use think\Session;
use think\paginator\driver\Bootstrap;
use think\Url;
class Questions extends Controller
{
    public function index()
    {
        $talk = new Talk();
        $if_login = $talk->check_login();
        if ($if_login == false){
            $this->assign('login_status','never');
        }else {
            $talk = new Talk();
            $never_read_num = $talk->never_read();
            $this->assign('never_read_num',$never_read_num);
            $this->assign('user_name',session('user_name','','app'));
            $this->assign('user_id',session('user_id','','app'));
            $this->assign('login_status','have');
            $this->assign('logout_url',url('index.php/app/talk/logout'));
            $this->assign('personal_url',url('index.php/app/main/personal_set'));
        }
        $keyword = input('get.keyword');
        if ($keyword == ''){
            $questions_list = db('questions')->field(['id','question_name'])->where('release','1')->paginate(10);
        }else {
            $questions_list = db('questions')->field(['id','question_name'])->where('release','1')->where('question_name','like','%'.$keyword.'%')->paginate(10,false,['query'=>request()->param()]);
        }
        $page = $questions_list->render();
        $this->assign('qusetions_list',$questions_list);
        $this->assign('page',$page);
        $this->assign('title','试题');
        $this->assign('active','questions');
        return $this->fetch('index');
    }
    
    public function questions_content()
    {
        $questions_id = input('get.questions_id');
        $user_id = Session::get('user_id','app');
        $user_info['user_id'] = $user_id;
        $user_info['user_name'] = Session::get('user_name','app');
        $user_question_org = db('questionslog')->where(['user_id'=>$user_id,'questions_id'=>$questions_id])->find();
        $user_question = $user_question_org['questions'];
        $question_list = db('questions')->where('id',$questions_id)->find();
        $question_name = $question_list['question_name'];
        $question_list = $question_list['questions'];
        $user_question = json_decode($user_question,true);
        $question_list = json_decode($question_list,true);
        if ($user_question_org == false){
            $this->assign('ifdone','never');
        }else {
            $score = $user_question_org['score'];
            foreach ($question_list as $qk => &$qv){
                foreach ($user_question as $uqk => $uqv){
                    if ($qv['question_id'] == $uqv['question_id']){
                        $qv['iftrue'] = $uqv['iftrue'];
                        switch ($qv['type']){
                            case '1':
                                $qv['user_answer'] = $uqv['answer'];
                                break;
                            case '2':
                                $qv['user_answer'] = explode('&', $uqv['answer']);
                                $qv['true_answer'] = explode('&', $qv['true_answer']);
                                break;
                            case '3':
                                $qv['user_answer'] = $uqv['answer'];
                                break;
                        }
                    }
                }
            }
            $this->assign('score',$score);
            $this->assign('ifdone','done');
        }
        $curpage = input('page') ? input('page') : 1;
        $data = $question_list;
        $data_to = array_chunk($data, 8);
        $question_list = array();
        if ($data_to){
        	$question_list = $data_to[$curpage-1];
        }else {
        	$question_list = null;
        }
        $question_list = Bootstrap::make($question_list, 8,$curpage,count($data),false,[
            'var_page' =>'page',
            'path' => Url::build('questions_content'),
            'query' => request()->param(),
            'fragment' => ''
        ]);
        $question_list->appends($_GET);
        $page = $question_list->render();
        $this->assign('title',$question_name);
        $this->assign('questions_id',$questions_id);
        $this->assign('question_list',$question_list);
        $this->assign('page',$page);
        $this->assign('user_info',$user_info);
        $this->view->engine->layout(false);
        return $this->fetch('questions_content');
    }
    
    public function save_user_answer()
    {
        $result = array('err' => '1','content' => '');
        if (Session::has('role','app') && Session::get('role','app') == 'admin'){
            $result['content'] = '您是管理员，不参与答题！';
            return $result;
        }
        $user_answer['user_id'] = Session::get('user_id','app');
        $user_answer['warehouse_id'] = Session::get('user_warehouse','app');
        $user_answer['questions_id'] = input('post.questions_id');
        $user_answer['questions'] = input('post.user_answer/a');
        $rules = [
            'user_id' => 'require|number',
            'questions_id' => 'require|number',
        ];
        $message = [
            'user_id' => '用户参数错误！',
            'questions_id' => '答卷参数错误!'
        ];
        $check_answer = $this->validate($user_answer, $rules,$message);
        if ($check_answer != 'true'){
            $result['content'] = $check_answer;
            return $result;
        }
        $questions_model = model('Questions');
        $save_user_answer = $questions_model->saveUserAnswer($user_answer);
        if ($save_user_answer == false){
            $result['content'] = '提交答卷失败！';
            return $result;
        }
        $result['err'] = '0';
        $result['content'] = '提交成功!';
        return $result;
    }
    
    public function questions_report()
    {
    	
    }
    
    
}